//
//  LGAddOutpatientRecordController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/3.
//  新增病历

#import "LGAddOutpatientRecordController.h"

//新增异常指标
#import "LGAddAnomalyNormController.h"
#import "LGAddReportHeaderView.h"
#import "LGInspectionAnomalyCell.h"
#import "DatePickerTwoView.h"
#import "LGPhotoPickerView.h"
#import "LGPickerView.h"
#import "LGNavigationController.h"
@interface LGAddOutpatientRecordController ()<DatePickerViewDelegate,LGPhotoPickerDelegate>
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIButton *dateTitle;
/**   */
@property (nonatomic , strong)UITextField *dateTF;
/**   */
@property (nonatomic , strong)UILabel *tipLabel;
/**   */
@property (nonatomic , strong)UIButton *programTitle;
/**   */
@property (nonatomic , strong)UITextField *programTF;
/**   */
@property (nonatomic , strong)UIButton *photoTitle;
/**   */
@property (nonatomic , strong)LGPhotoPickerView *photoView;
/**   */
@property (nonatomic , strong)UIView *bottomView;
/**   */
@property (nonatomic , strong)UIButton *saveBtn;
/**   */
@property (nonatomic , strong)UIButton *deleteBtn;
/**   */
@property (nonatomic , strong)NSArray *typeArray;
/**   */
@property (nonatomic , strong)NSArray *pickerArray;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , strong)NSMutableArray *photoArray;
/**   */
@property (nonatomic , copy)NSString *idString;
/**   */
@property (nonatomic , copy)NSString *codeString;
@end

@implementation LGAddOutpatientRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"新增病历";
    if(_reportid && _reportid.length>0){
        //编辑
        self.navigationView.title = @"编辑病历";
        self.tipLabel.hidden = YES;
        self.deleteBtn.hidden = NO;
        [self requestData];
    }else{
        self.reportid = @"";
        self.tipLabel.hidden = NO;
        self.deleteBtn.hidden = YES;
    }
    [self createSubView];
    [self.view layoutSubviews];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [LGNavigationController initialize];
    [self resetNavigationTheme];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Visitrecords/visitDetail" parameters:@{@"record_id":self.reportid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dateTF.text = LGNSString(returnData[@"visit_date"]);
            NSString *type = LGNSString(returnData[@"visit_record_type"]);
            self.programTF.text = [type isEqualToString:@"1"]?@"门诊记录":@"住院记录";
            self.photoView.urlArray = returnData[@"weburls"];
            self.codeString = type;
        }
    } error:^(NSError *error) {
        
    }];
}


//选择日期、病历类型
-(void)selectViewTouchAction:(UIButton *)sender{
    if(sender.superview == self.dateTF){
        //选择日期
        DatePickerTwoView *dateView = [[DatePickerTwoView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        dateView.title = @"请选择日期";
        dateView.delegate = self;
        [[UIApplication sharedApplication].keyWindow addSubview:dateView];
        
    }else if (sender.superview == self.programTF){
        //选择检查项目
        LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.pickArry = @[@[@"门诊记录",@"住院记录"]];
        pickerView.selectStr = self.programTF.text;
        [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
        __weak typeof(self) weakSelf = self;
        pickerView.selectPickViewResult = ^(NSArray *resultArray) {
            weakSelf.programTF.text = LGNSString(resultArray[0]);
            weakSelf.codeString = [LGNSString(resultArray[0]) isEqualToString:@"门诊记录"]?@"1":@"2";
        };
    }
}

-(void)sendSelectDate:(NSString *)date index:(NSInteger)index{
    self.dateTF.text = date;
}

//图片View高度变化
-(void)imagePickerHeightChanged:(CGFloat)height{
    self.photoView.frame = CGRectMake(viewPix(16), kMaxY(self.photoTitle.frame)+viewPix(8), Screen_W-viewPix(64), height);
    CGFloat totalH = kMaxY(self.photoView.frame)+viewPix(40);
    self.scrollView.contentSize = CGSizeMake(Screen_W, totalH+viewPix(10));
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(totalH));
    }];
}

//保存
-(void)saveBtnAction{
    if(self.dateTF.text.length == 0){
        [TooltipView showMessage:@"请选择日期" offset:0];
        return;
    }
    if(self.programTF.text.length == 0){
        [TooltipView showMessage:@"请选择病历类型" offset:0];
        return;
    }
    if(self.photoView.allImageArray.count == 0){
        [TooltipView showMessage:@"请上传图片" offset:0];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [RequestUtil uploadImage:@"/doctor/upload/image" parameters:@{} withSign:YES indexName:@"photo[]" imageArray:self.photoView.allImageArray success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            NSArray *dataArray = returnData;
            NSMutableArray *photoArray = [NSMutableArray array];
            for (NSDictionary *tempDic in dataArray) {
                [photoArray addObject:@{@"url":LGNSString(tempDic[@"uri"])}];
            }
            [RequestUtil withPOST:@"/doctor/PhysicalRecord/reportFilesUpload" parameters:@{@"patient_id":self.patientid,@"record_id":self.reportid,@"recordImages":[ObjectTool objectToJson:photoArray],@"exams_date":self.dateTF.text,@"physical_record_type":self.codeString} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                if(code == 200){
                    [TooltipView showMessage:@"保存成功" offset:0];
                    if(self.addReportSuccess){
                        self.addReportSuccess();
                    }
                    [self.navigationController popViewControllerAnimated:YES];
                }else{
                    [TooltipView showMessage:message offset:0];
                }
            } error:^(NSError *error) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            }];
        }else{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

//删除
-(void)deleteBtnAction{
    __weak typeof(self) weakSelf = self;
    [LGAlertTool alertWithTitle:@"确定要删除此病历吗？" sureAction:^{
        [RequestUtil withPOST:@"/doctor/Visitrecords/visitDel" parameters:@{@"record_id":weakSelf.reportid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                if(weakSelf.addReportSuccess){
                    weakSelf.addReportSuccess();
                }
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            [TooltipView showMessage:@"删除失败，请重试" offset:0];
        }];
    }];
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    CGFloat topMargin = (_reportid && _reportid.length>0)?viewPix(12):viewPix(33);
    self.programTitle.frame = CGRectMake(viewPix(16), kMaxY(self.dateTF.frame)+topMargin, viewPix(150), viewPix(21));
    self.programTF.frame = CGRectMake(viewPix(16), kMaxY(self.programTitle.frame)+viewPix(8), Screen_W-viewPix(64), viewPix(40));
    self.photoTitle.frame = CGRectMake(viewPix(16), kMaxY(self.programTF.frame)+viewPix(12), viewPix(150), viewPix(21));
    self.photoView.frame = CGRectMake(viewPix(16), kMaxY(self.photoTitle.frame)+viewPix(8), Screen_W-viewPix(64), self.photoView.bounds.size.height);
    CGFloat leftMargin = (_reportid && _reportid.length>0)?(Screen_W-viewPix(45))/2.0+viewPix(26):viewPix(17);
    CGFloat saveWidth = (_reportid && _reportid.length>0)?(Screen_W-viewPix(45))/2.0:Screen_W-viewPix(34);
    self.saveBtn.frame = CGRectMake(leftMargin, viewPix(12), saveWidth, viewPix(46));
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.baseView];
    [self.view addSubview:self.bottomView];
    [self.bottomView addSubview:self.saveBtn];
    [self.bottomView addSubview:self.deleteBtn];
    [self.baseView addSubview:self.dateTitle];
    [self.baseView addSubview:self.dateTF];
    [self.baseView addSubview:self.tipLabel];
    [self.baseView addSubview:self.programTitle];
    [self.baseView addSubview:self.programTF];
    [self.baseView addSubview:self.photoTitle];
    [self.baseView addSubview:self.photoView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView).offset(viewPix(16));
        make.left.equalTo(self.view).offset(viewPix(16));
        make.right.equalTo(self.view).offset(-viewPix(16));
        make.height.equalTo(@(viewPix(350)));
    }];
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight-viewPix(84)-bottomSafeBarHeight/4.0)];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.contentSize = CGSizeMake(Screen_W, Screen_H-topBarHeight);
    }
    return _scrollView;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UIButton *)dateTitle{
    if(!_dateTitle){
        _dateTitle = [UIButton buttonWithTitle:@"日期" titleFont:LGFontWeight(15, UIFontWeightMedium) textColor:@"#333333" imageName:@"必选项"];
        _dateTitle.frame = CGRectMake(viewPix(16), viewPix(14), viewPix(150), viewPix(21));
        _dateTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _dateTitle.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        _dateTitle.userInteractionEnabled = NO;
    }
    return _dateTitle;
}

-(UITextField *)dateTF{
    if(!_dateTF){
        _dateTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), kMaxY(self.dateTitle.frame)+viewPix(8), Screen_W-viewPix(64), viewPix(40))];
        _dateTF.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        _dateTF.layer.borderWidth = 1;
        _dateTF.layer.cornerRadius = 7;
        _dateTF.textColor = [UIColor colorWithString:@"#333333"];
        _dateTF.textAlignment = NSTextAlignmentLeft;
        _dateTF.placeholder = @"请选择日期";
        _dateTF.font = LGFont(14);
        _dateTF.leftViewMode = UITextFieldViewModeAlways;
        _dateTF.rightViewMode = UITextFieldViewModeAlways;
        _dateTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(12), viewPix(40))];
        _dateTF.rightView = [self rightView];
        [_dateTF addSubview:[self actionButton]];
    }
    return _dateTF;
}

-(UILabel *)tipLabel{
    if(!_tipLabel){
        _tipLabel = [UILabel lableWithText:@"请选择日期后保存" colorString:@"#DB453A" textFont:LGFont(12) textAlignment:NSTextAlignmentLeft lines:1];
        _tipLabel.frame = CGRectMake(viewPix(16), kMaxY(self.dateTF.frame)+viewPix(4), viewPix(120), viewPix(20));
    }
    return _tipLabel;
}

-(UIButton *)programTitle{
    if(!_programTitle){
        _programTitle = [UIButton buttonWithTitle:@"病历类型" titleFont:LGFontWeight(15, UIFontWeightMedium) textColor:@"#333333" imageName:@"必选项"];
        _programTitle.frame = CGRectMake(viewPix(16), kMaxY(self.dateTF.frame)+viewPix(33), viewPix(150), viewPix(21));
        _programTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _programTitle.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        _programTitle.userInteractionEnabled = NO;
    }
    return _programTitle;
}


-(UITextField *)programTF{
    if(!_programTF){
        _programTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), kMaxY(self.programTitle.frame)+viewPix(8), Screen_W-viewPix(64), viewPix(40))];
        _programTF.textColor = [UIColor colorWithString:@"#333333"];
        _programTF.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        _programTF.layer.borderWidth = 1;
        _programTF.layer.cornerRadius = 7;
        _programTF.textAlignment = NSTextAlignmentLeft;
        _programTF.placeholder = @"请选择病历类型";
        _programTF.font = LGFont(14);
        _programTF.leftViewMode = UITextFieldViewModeAlways;
        _programTF.rightViewMode = UITextFieldViewModeAlways;
        _programTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(12), viewPix(40))];
        _programTF.rightView = [self rightView];
        [_programTF addSubview:[self actionButton]];
    }
    return _programTF;
}

-(UIButton *)photoTitle{
    if(!_photoTitle){
        _photoTitle = [UIButton buttonWithTitle:@"上传图片" titleFont:LGFontWeight(15, UIFontWeightMedium) textColor:@"#333333" imageName:@"必选项"];
        _photoTitle.frame = CGRectMake(viewPix(16), kMaxY(self.programTF.frame)+viewPix(12), viewPix(150), viewPix(21));
        _photoTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _photoTitle.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        _photoTitle.userInteractionEnabled = NO;
    }
    return _photoTitle;
}

-(LGPhotoPickerView *)photoView{
    if(!_photoView){
        _photoView = [[LGPhotoPickerView alloc]initWithFrame:CGRectMake(viewPix(16), kMaxY(self.photoTitle.frame)+viewPix(8), Screen_W-viewPix(64), viewPix(72))];
        _photoView.delegate = self;
        _photoView.maxCount = 20;
    }
    return _photoView;
}

    
-(UIView *)rightView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(30), viewPix(40))];
    UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip12"]];
    tipView.frame = CGRectMake(viewPix(6), viewPix(14), viewPix(12), viewPix(12));
    [baseView addSubview:tipView];
    return baseView;
}
    
-(UIButton *)actionButton{
    UIButton *actionBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    actionBtn.frame = CGRectMake(0, 0, Screen_W-viewPix(64), viewPix(40));
    [actionBtn addTarget:self action:@selector(selectViewTouchAction:) forControlEvents:UIControlEventTouchUpInside];
    return actionBtn;
}
    

-(UIView *)bottomView{
    if(!_bottomView){
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_H-viewPix(84)-bottomSafeBarHeight/4.0, Screen_W, viewPix(84)+bottomSafeBarHeight/4.0)];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

-(UIButton *)saveBtn{
    if(!_saveBtn){
        _saveBtn = [UIButton buttonWithTitle:@"保存" topMargin:viewPix(12)];
        [_saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

-(UIButton *)deleteBtn{
    if(!_deleteBtn){
        _deleteBtn = [UIButton buttonWithTitle:@"删除" titleFont:LGFontWeight(16, UIFontWeightSemibold) normalColor:@"#DD3030" selectColor:@"#DD3030"];
        _deleteBtn.frame = CGRectMake(viewPix(17), viewPix(12), (Screen_W-viewPix(45))/2.0, viewPix(46));
        _deleteBtn.layer.borderColor = [UIColor colorWithString:@"#DD3030"].CGColor;
        _deleteBtn.layer.borderWidth = 1.0;
        _deleteBtn.layer.cornerRadius = 10;
        _deleteBtn.hidden = YES;
        [_deleteBtn addTarget:self action:@selector(deleteBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

@end
